<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    //
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table = 'products';

    protected $fillable = [
        'bistro_id', 'img', 'status', 'name', 'discount', 'price', 'sale_count', 'zan_number', 'description', 'product_category_id'
    ];


    public function scopeName($query, $name) {
        if(!empty($name)) {
            return $query->where('name', 'like', '%'.$name.'%');
        }
        return $query;
    }

    //按类型查询区间
    public function scopeTypeOrder($query, $type, $start, $end) {
        switch($type){
            case 'sale_count':
                $type = 'sale_count';
                break;
            case 'price':
                $type = 'price';
                break;
            case 'discount':
                $type = 'discount';
                break;
            case 'zan_number':
                $type = 'zan_number';
                break;
            default:
                $type = 'sale_count';
        }
        if(!empty($start) && !empty($end)) {
            $query->where($type, '>=', $start)->where($type, '<=', $end);
        }
        if(!empty($start)) {
            $query->where($type, '>=', $start);
        }
        if(!empty($end)) {
            $query->where($type, '<=', $end);
        }
        return $query;
    }

    public function scopeFindCategory() {

    }

    public function daysale() {
        return $this->hasMany('\App\Model\ProductSaleDay');
    }

    public function monthSale() {
        return $this->hasMany('\App\Model\ProductSaleMonth');
    }

    public function category() {
        return $this->belongsTo('\App\Model\ProductCategory', 'product_category_id','id');
    }
}
